package com.maxd.controller.douyin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.CommonInfo;
import com.maxd.model.Req;
import com.maxd.model.UserInfo;
import com.maxd.respository.common.CommonRepository;
import com.maxd.respository.userinfo.UserJpaRepository;
import com.maxd.respository.userinfo.UserRepository;
import com.maxd.utils.Config;
import com.maxd.utils.HttpClientUtil;
//import com.taobao.api.ApiException;
//import com.taobao.api.DefaultTaobaoClient;
//import com.taobao.api.TaobaoClient;
//import com.taobao.api.request.*;
//import com.taobao.api.response.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "抖音api管理", tags = {"抖音api管理"})
@Slf4j
@RequestMapping(value = "/douyin")
public class dyController {

    @Autowired
    private CommonRepository commonRepository;
    @Autowired
    private UserRepository userRepository;

    @IgnoreSecurity
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("抖音商品查询")
    @ResponseBody
    public Result list(int page,@ApiParam("请在1,2,5,10,20,50中选择一个数值返回") int size,String cateId,
                       @ApiParam("排序 0综合、1销量倒序、2销量升序、3价格倒序、4价格升序" ) String sort,
                       @ApiParam("搜索商品名称")String keyword) {
        CommonInfo appkey = commonRepository.findOne(238);
        Map<String,String> map=new HashMap<>();
        map.put("apikey",String.valueOf(appkey.getValue()));
        map.put("back",String.valueOf(size));
        map.put("min_id",String.valueOf(page));
        if(StringUtils.isNotEmpty(sort)){
            map.put("sort",sort);
        }
        if(StringUtils.isNotEmpty(cateId)){
            map.put("cate_id",cateId);
        }
        if(StringUtils.isNotEmpty(keyword)){
            map.put("keyword",keyword);
        }
        String body = HttpClientUtil.doGet("https://v2.api.haodanku.com/dy_item_list", map);
        return ResultUtil.success(JSONObject.parseObject(body));
    }

    @IgnoreSecurity
    @RequestMapping(value = "/typeList", method = RequestMethod.GET)
    @ApiOperation("类目")
    @ResponseBody
    public Result typeList(@ApiParam("类型：1 个性化福利清单-商品类目、2 个性化福利清单-线报类目、" +
            "3 福利清单-商品类目、4 抖音商城-商品类目、5 凑单满减-满减类型、6 虎年单品类目") String type,
                           @ApiParam("个性化福利清单-商品子类目ID") String labelId) {
        CommonInfo appkey = commonRepository.findOne(238);
        Map<String,String> map=new HashMap<>();
        map.put("apikey",String.valueOf(appkey.getValue()));
        map.put("type",type);
        if(StringUtils.isNotEmpty(labelId)){
            map.put("label_id",labelId);
        }
        String body = HttpClientUtil.doGet("https://v2.api.haodanku.com/category_list", map);
        return ResultUtil.success(JSONObject.parseObject(body));
    }

    @IgnoreSecurity
    @RequestMapping(value = "/getLink", method = RequestMethod.GET)
    @ApiOperation("转链")
    @ResponseBody
    public Result getLink(@ApiParam("商品id") String itemid,@ApiParam("渠道id") String channel) {
        CommonInfo appkey = commonRepository.findOne(238);
        if(StringUtils.isEmpty(channel)){
            channel=commonRepository.findOne(4).getValue();
        }
        UserInfo userByRelationId=null;
        if(channel.length()==6){
            userByRelationId = userRepository.getUserByInvitationCode(channel);
        }else{
            userByRelationId = userRepository.getUserByRelationId(channel);
        }
        Map<String,String> map=new HashMap<>();
        map.put("apikey",String.valueOf(appkey.getValue()));
        map.put("itemid",itemid);
        map.put("channel",userByRelationId.getInvitationCode());
        String body = HttpClientUtil.doPost("https://v2.api.haodanku.com/get_dyitem_link", map);
        return ResultUtil.success(JSONObject.parseObject(body));
    }


    @IgnoreSecurity
    @RequestMapping(value = "/dyAnalyzeCode", method = RequestMethod.GET)
    @ApiOperation("口令解析")
    @ResponseBody
    public Result dyAnalyzeCode(@ApiParam("口令/链接等内容") String content) {
        CommonInfo appkey = commonRepository.findOne(238);
        Map<String,String> map=new HashMap<>();
        map.put("apikey",String.valueOf(appkey.getValue()));
        map.put("content",content);
        String body = HttpClientUtil.doPost("https://v2.api.haodanku.com/dy_analyze_code", map);
        return ResultUtil.success(JSONObject.parseObject(body));
    }



}
